package com.corpcredit.webapp.action;

import java.io.Serializable;

import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Td;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class TdForm extends BasePage implements Serializable {
    private GenericManager<Td, Long> tdManager;
    private Td td = new Td();
    private Long id;
    
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }
    public void setTdManager(GenericManager<Td, Long> tdManager) {
        this.tdManager = tdManager;
    }

    public Td getTd() {
        return td;
    }

    public void setTd(Td td) {
        this.td = td;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        tdManager.remove(td.getId());
        addMessage("td.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            td = tdManager.get(id);
        } else {
            td = new Td();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (td.getId() == null || td.getId() == 0);
        // set the related jbqk as a foreign key.
        td.setJbqk( jbqk );
        tdManager.save(td);

        String key = (isNew) ? "td.added" : "td.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }
} 